package com.coocaa.app.lib.cleaner.deleter.utils;

import android.content.Context;
import android.content.pm.IPackageDataObserver;
import android.content.pm.PackageManager;
import android.os.RemoteException;

import com.coocaa.app.lib.cleaner.deleter.pkgdata.impl.SpecialCleaner;

import java.lang.reflect.Method;

/**
 * Created by Eric on 2017/4/13.
 */

public class CleanUtils {
    private Context mContext = null;

    public CleanUtils(Context mContext) {
        this.mContext = mContext;
    }

    public boolean needSpecialClean(String pkgName){
        return SpecialCleaner.needSpecialClean(pkgName);
    }

    public interface PackageCacheClearListener {
        void onDeleteCacheComplete(String packageName,
                                          boolean succeeded, String errorMsg);
    }

    public interface PackageDataClearListener{
        void onDeleteDataComplete(String packageName,
                                   boolean succeeded, String errorMsg);
    }

    public interface SpecialPackageDataClearListener{
        void onDeleteDataComplete(String packageName,
                                  boolean succeeded, long size);
    }

    public void deleteApplicationCacheFile(final String pname,
                                           final PackageCacheClearListener listener) {
        PackageManager pm = mContext.getPackageManager();
        try {
            Method deleteApplication = pm.getClass().getMethod(
                    "deleteApplicationCacheFiles", String.class,
                    IPackageDataObserver.class);
            deleteApplication.invoke(pm, pname,
                    new IPackageDataObserver.Stub() {
                        @Override
                        public void onRemoveCompleted(String packageName,
                                                      boolean succeeded) throws RemoteException {
                            // TODO Auto-generated method stub
                            if (listener != null) {
                                listener.onDeleteCacheComplete(packageName,
                                        succeeded, null);
                            }
                        }
                    });
        } catch (Exception e) {
            if (listener != null) {
                listener.onDeleteCacheComplete(pname, false, e.toString());
            }
            e.printStackTrace();
        }
    }

    public void deleteApplicationUserData(final String pname,
                                          final PackageDataClearListener listener) {
        PackageManager pm = mContext.getPackageManager();
        try {
            Method deleteApplication = pm.getClass().getMethod(
                    "clearApplicationUserData", String.class,
                    IPackageDataObserver.class);
            deleteApplication.invoke(pm, pname,
                    new IPackageDataObserver.Stub() {
                        @Override
                        public void onRemoveCompleted(String packageName,
                                                      boolean succeeded) throws RemoteException {
                            // TODO Auto-generated method stub
                            if (listener != null) {
                                listener.onDeleteDataComplete(packageName,
                                        succeeded, null);
                            }
                        }
                    });
        } catch (Exception e) {
            if (listener != null) {
                listener.onDeleteDataComplete(pname, false, e.toString());
            }
            e.printStackTrace();
        }
    }

    public void deleteSpecialApplicationData(String pname,SpecialPackageDataClearListener listener) {
        long size = SpecialCleaner.deleteSpecialApplicationData(pname);
        if (listener != null) {
            if(size >= 0)
                listener.onDeleteDataComplete(pname, true, size);
            else
                listener.onDeleteDataComplete(pname, false, -1);
        }
    }

}
